const { createProxyMiddleware } = require("http-proxy-middleware");
const bodyParser = require("body-parser");
const multipart = require("connect-multiparty");
const multipartMiddleware = multipart();
const OSS = require("ali-oss");
const path = require("path");
const client = new OSS({
  region: "oss-cn-shanghai",
  accessKeyId: "LTAI5t8bDur6gYtCJvgFfVKb",
  accessKeySecret: "sAPa8PyL7VK8ltJedAbSJBtJOH5EIF",
  bucket: "2007a-1",
});
module.exports = app => {
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: false }));
  app.use(
    "/api",
    createProxyMiddleware({
      target: "http://localhost:7001",
      changeOrigin: true,
      pathRewrite: { "^/api": "" },
    })
  );
  app.post("/upload/oss", multipartMiddleware, async (req, res) => {
    console.log(req.body, req.files);
    req.files.chunk.path;
    req.files.chunk.name;
    const result = await client.put(
      req.files.chunk.name,
      path.normalize(req.files.chunk.path)
    );
    res.send({
      code: 200,
      message: "上传成功",
      data: result,
    });
  });
};
